package com.naydenov.smscarparking;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	//TAG used for Log.X
	private String TAG = this.getClass().getName();
	//Pending Intent and BroadcastReceiver used by SmsManager
	private PendingIntent pi_sent;
	private String SENT="SMS_SENT";
	private BroadcastReceiver sentReceiver;
	//Store number and sms text so anonymous class
	//can access them
	private String number;
	private String msg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		//setup buttons labels
		setButtonLabels();
		
		//setup event handlers
		Button btn = (Button)findViewById(R.id.btnSend1);
		btn.setOnClickListener(new OnClickListener() {
		    @Override
			public void onClick(View v) {
				// Use application's Context
				SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
				String number = sp.getString("tel1", "");
				String sms = sp.getString("sms1", "");
				sendSms(number, sms);
				
			}
		});
		btn = (Button)findViewById(R.id.btnSend2);
		btn.setOnClickListener(new OnClickListener() {
		    @Override
			public void onClick(View v) {
		    	// Use application's Context
				SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
				String number = sp.getString("tel2", "");
				String sms = sp.getString("sms2", "");
				sendSms(number, sms);
				
			}
		});
		btn = (Button)findViewById(R.id.btnSend3);
		btn.setOnClickListener(new OnClickListener() {
		    @Override
			public void onClick(View v) {
		    	// Use application's Context
				SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
				String number = sp.getString("tel3", "");
				String sms = sp.getString("sms3", "");
				sendSms(number, sms);
				
			}
		});
		btn = (Button)findViewById(R.id.btnSettings);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), SettingsActivity.class);
				startActivity(intent);
				
			}
		});
		btn = (Button)findViewById(R.id.btnExit);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		//setup pending intent when sms is sent
		pi_sent = PendingIntent.getBroadcast(this,0, new Intent(SENT), 0);
	}
	private void setButtonLabels(){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		String label;
		Button btn = (Button)findViewById(R.id.btnSend1);
		label = sp.getString("label1", "");
		if(label == "")
			btn.setText(getResources().getString(R.string.zone1));
		else
			btn.setText(label);
		btn = (Button)findViewById(R.id.btnSend2);
		label = sp.getString("label2", "");
		if(label == "")
			btn.setText(getResources().getString(R.string.zone2));
		else
			btn.setText(label);
		btn = (Button)findViewById(R.id.btnSend3);
		label = sp.getString("label3", "");
		if(label == "")
			btn.setText(getResources().getString(R.string.zone3));
		else
			btn.setText(label);
			
	}
	public void alertMsg(int strid){
		//use Context from Activity
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(strid);
		builder.setPositiveButton(R.string.ok, null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	class SmsSendOnClickListener implements DialogInterface.OnClickListener{
		public void onClick(DialogInterface dialog, int which) {
			try{
				SmsManager sm = SmsManager.getDefault();
				sm.sendTextMessage(number, null, msg, pi_sent, null);
				Log.d(TAG, "Sms send");
			}catch(Exception e){
				alertMsg(R.string.smsfail);
			}
			
		}
	}
	public void sendSms(String number, String msg)
	{
		if(number==""){
			alertMsg(R.string.notel);
			return;
		}
		Log.d(TAG, String.format("number:%s",number));
		if(msg==""){
			alertMsg(R.string.nosms);
			return;
		}
		Log.d(TAG, String.format("sms:%s", msg));
		this.number = number;
		this.msg = msg;
		SmsSendOnClickListener sendListener = new SmsSendOnClickListener();
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.verifysend);
		builder.setPositiveButton(R.string.ok, sendListener);
		builder.setNegativeButton(R.string.cancel, null);
		builder.create().show();
			
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	public void onResume(){
		super.onResume();
		
		sentReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// AlertDialog is sent asynchronously, seems safe
				// to be fired from BroadcastReceiver
				switch(getResultCode()){
				case Activity.RESULT_OK: 
					alertMsg(R.string.sent);break;
				 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    alertMsg(R.string.genericfail);break;
                 case SmsManager.RESULT_ERROR_NO_SERVICE:
                	 alertMsg(R.string.noservice);break;
                 case SmsManager.RESULT_ERROR_NULL_PDU:
                	 alertMsg(R.string.nullpdu);break;
                 case SmsManager.RESULT_ERROR_RADIO_OFF:
                     alertMsg(R.string.radiooff);break;
                  
				}
			}
		};
		
		registerReceiver(sentReceiver, new IntentFilter(SENT));
		
		//button labels can be changes in Settings activity
		setButtonLabels();
	}
	public void onPause(){
		super.onPause();
		unregisterReceiver(sentReceiver);
	}
}
